﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Http.Results;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.BLL;
using ZHDJ.JoinParty.BLL;

namespace ZHDJ.AppInterface.Controllers
{
    public class JoinApplycationController : ApiController
    {
        #region 入党申请
        [HttpPost]
        public ReturnResult Save(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            string LoginID = Convert.ToString(obj.LoginID);
            PartyOrgMember member = InterfaceHelper.GetPartyBuildingUserModel(LoginID);

            #region 个人中心赋值
            member.ModifyTimeEx = DateTime.Now;
            member.BranchID = Convert.ToString(obj.BranchID);
            member.Gender = Convert.ToString(obj.Gender);
            member.IdentityCard = Convert.ToString(obj.IdentityCard);
            if (!string.IsNullOrEmpty(Convert.ToString(obj.Birthday)))
                member.Birthday = Convert.ToDateTime(obj.Birthday);
            member.Nation = Convert.ToString(obj.Nation);
            member.NativePlace = Convert.ToString(obj.NativePlace);
            member.Educational = Convert.ToString(obj.Educational);
            member.MobileNo = Convert.ToString(obj.MobileNo);
            member.Email = Convert.ToString(obj.Email);
            member.QQNumber = Convert.ToString(obj.QQNumber);
            member.WeiXinNo = Convert.ToString(obj.WeiXinNo);
            member.Address = Convert.ToString(obj.Address);
            member.WorkUnit = Convert.ToString(obj.WorkUnit);
            member.WorkAddress = Convert.ToString(obj.WorkAddress);
            #endregion

            #region 入党申请赋值
            PartyJoinApplycation pja = new PartyJoinApplycation();
            //申请ID
            pja.AppllicationID = StringHelper.NewID;
            pja.Content = Convert.ToString(obj.heContent);
            pja.BranchID = Convert.ToString(obj.BranchID);
            pja.LoginID = LoginID;
            pja.UserName = Convert.ToString(obj.UserName);
            pja.CreateTime = Convert.ToDateTime(obj.ApplyTime);
            pja.CreateUserID = LoginID;
            pja.BeforeName = Convert.ToString(obj.BeforeName);
            pja.Nature = Convert.ToString(obj.Nature);
            if (Convert.ToString(obj.Status) == "0")  //0:表示保存，1：表示提交
                pja.Status = (int)Enum_ApplyStatus.草稿;
            else
                pja.Status = (int)Enum_ApplyStatus.申请中;
            #endregion

            #region 修改个人资料
            //验证身份证与手机号码的唯一性
            if (ModuleUtil.CheckLoginIDCount(member.IdentityCard, LoginID) && member.IdentityCard != "")
            {
                result.code = "201";
                result.msg = string.Format("你输入的身份证：{0}已经存在，请确认！", member.IdentityCard);
                return result;
            }
            if (ModuleUtil.CheckLoginIDCount(member.MobileNo, LoginID) && member.MobileNo != "")
            {
                result.code = "201";
                result.msg = string.Format("你输入的手机号码：{0}已经存在，请确认！", member.MobileNo);
                return result;
            }
            if (InterfaceHelper.UpdatePartyBuildingUserModel(member) == 0)
            {
                result.code = "201";
                result.msg = "修改个人资料失败";
                return result;
            }
            #endregion

            #region 提交入党申请
            if (InterfaceHelper.SaveJoinApplycation(pja) == 0)
            {
                result.code = "201";
                result.msg = "入党申请提交失败";
                return result;
            }

            if (!InterfaceHelper.IsApplyOver(pja.AppllicationID))
                InterfaceHelper.CreateProcesStep(pja.AppllicationID, member.UserBranchId, LoginID, member.UserTrueName);
            else
            {
                result.code = "201";
                result.msg = "你已经提交了申请，请确认";
                return result;
            }
            result.code = "200";
            result.msg = "入党申请提交成功";
            return result;
            #endregion
        }
        #endregion

        #region 获取入党申请
        [HttpPost]
        public ReturnResult GetJoinApplycation(dynamic obj)
        {
            DataTable dt = new DataTable();
            IList<PartyJoinApplycation> list = new List<PartyJoinApplycation>();
            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            string loginId = Convert.ToString(obj.CreateBy);

            dt = InterfaceHelper.GetJoinApplycation(pageIndex, pageSize, loginId);
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyJoinApplycation>.ConvertToModel(dt);

            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = dt.Rows.Count.ToString()
            };
        }
        #endregion
    }
}
